package com.test.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendFile {

	public static void main(String[] args) {
		// if (args.length != 5) {
		// System.out.println("usage: java SendFile <to> <from> <smtp> <file> true|false");
		// System.exit(1);
		// }

		long startTime = System.currentTimeMillis();
		String to = "ling-piao.kong@hp.com";
		//String to = "ling-piao.kong@hp.com"; // args[0];
		String from = "ling-piao.kong@hp.com"; // "ling_piao_kong@sina.cn";//
												// args[1];
		// String host = args[2];
		//String filename = "C:/Temp/SLA_DETAILS_REPORTS/123456789/123456789_1/SLA_DETAILS_2010-7-4.pdf"; // args[3];
		boolean debug = true;// Boolean.valueOf(args[4]).booleanValue();
		//String msgText1 = "<h1>This is the SLA_DETAILS report.</h1><br> another line";
		String msgText1 = "test</br>This is the SLA_DETAILS report.\n Another line";
		String subject = "Test html";

		// create some properties and get the default Session
		Properties props = new Properties();

		// props.put("mail.smtp.host", host);
		// props.put("mail.smtp.host", "smtp.gmail.com");
		// props.put("mail.smtp.host", "GVW1343EXA.americas.hpqcorp.net");
		props.put("mail.smtp.host", "smtp.hp.com");

		props.put("mail.transport.protocol", "smtp"); // 之前没加
		props.put("mail.smtp.starttls.enable", "false"); // 之前没加
		// props.put("mail.smtp.host", "gmail-smtp.l.google.com");
		props.put("mail.smtp.auth", "false");

		/*
		 * props.setProperty("mail.smtp.socketFactory.class",
		 * "javax.net.ssl.SSLSocketFactory");
		 * props.setProperty("mail.smtp.socketFactory.fallback", "false");
		 * props.setProperty("mail.smtp.port", "465");
		 * props.setProperty("mail.smtp.socketFactory.port", "465");
		 */

		//		Session session = Session.getInstance(props, new Authenticator() {
		//			protected PasswordAuthentication getPasswordAuthentication() {
		//				return new PasswordAuthentication("ling_piao_kong@sina.cn", "11111111");
		//			}
		//		});

		Session session = Session.getDefaultInstance(props);
		session.setDebug(debug);

		try {
			// create a message
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(subject);

			// create and fill the first message part
			MimeBodyPart mbp1 = new MimeBodyPart();
			//mbp1.setText(msgText1);
			mbp1.setContent(msgText1.replaceAll("\\n", "<br>"), "text/html;charset=utf-8");

			// create the second message part
			//MimeBodyPart mbp2 = new MimeBodyPart();

			// attach the file to the message
			//mbp2.attachFile(filename);

			/*
			 * Use the following approach instead of the above line if you want
			 * to control the MIME type of the attached file. Normally you
			 * should never need to do this.
			 */
//			FileDataSource fds = new FileDataSource("c:/bb.jpg");
//			mbp2.setContentID("<mylog>");
//			mbp2.setDisposition(Part.INLINE);
//			mbp2.setDataHandler(new DataHandler(fds));
//			mbp2.setFileName(fds.getName());

			// create the Multipart and add its parts to it
			Multipart mp = new MimeMultipart();
			mp.addBodyPart(mbp1);
			//mp.addBodyPart(mbp2);

			//msg.setContent(msgText1, "text/html;charset=utf-8");

			// add the Multipart to the message
			msg.setContent(mp);

			// set the Date: header
			msg.setSentDate(new Date());

			/*
			 * If you want to control the Content-Transfer-Encoding of the
			 * attached file, do the following. Normally you should never need
			 * to do this.
			 * 
			 * msg.saveChanges(); mbp2.setHeader("Content-Transfer-Encoding",
			 * "base64");
			 */

			// send the message
			Transport.send(msg);

		} catch (Exception mex) {
			mex.printStackTrace();
		}

		System.out.println("Finished, cost: " + (System.currentTimeMillis() - startTime));

	}
}
